package com.leesche.zcsp.node.web.controller.web.v2.manage;

import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.controller.web.v2.admin.WebAdminController;
import com.leesche.zcsp.node.web.model.SysUser;
import com.leesche.zcsp.node.web.service.SysUserService;
import com.leesche.zcsp.node.web.service.impl.data.read.QuerySysUserCondition;
import com.leesche.zcsp.node.web.service.privilege.PrivilegeService;
import com.leesche.zcsp.node.web.vo.Principal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * Created by kaider on 2017/1/19.
 */
@Controller
@RequestMapping("/v2/factory/manage")
public class WebFactoryManageController extends WebAdminController {

    private final static Logger LOGGER = LoggerFactory.getLogger(WebFactoryManageController.class);

    private static final String SHOW_VIEW = "web/v2/manage/factory/showView";

    @Autowired
    protected SysUserService userService;
    @Autowired
    private PrivilegeService privilegeService;

    /**
     * 二维码
     * @param model
     * @return
     */
    @RequestMapping(value = "/showView", method = RequestMethod.GET)
    public String showView(
        Model model
    ){
        String shortCode = getUserShortCode();
        model.addAttribute("shortCode",shortCode);
        model.addAttribute("view",1);
        model.addAttribute("cur",1);
        return SHOW_VIEW;
    }

    /**
     * 工厂简码
     * @param model
     * @return
     */
    @RequestMapping(value = "/showShortCodeView", method = RequestMethod.GET)
    public String showShortCodeView(
        Model model
    ){
        String shortCode = getUserShortCode();
        model.addAttribute("shortCode",shortCode);
        model.addAttribute("view",2);
        model.addAttribute("cur",2);
        return SHOW_VIEW;
    }

    /**
     * 更换手机
     * @param model
     * @return
     */
    @RequestMapping(value = "/showMobileView", method = RequestMethod.GET)
    public String showMobileView(
        Model model
    ){
        model.addAttribute("view",3);
        model.addAttribute("cur",3);
        return SHOW_VIEW;
    }

    /**
     * 更换密码
     * @param model
     * @return
     */
    @RequestMapping(value = "/showPassWordView", method = RequestMethod.GET)
    public String showPassWordView(
        Model model
    ){
        model.addAttribute("view",4);
        model.addAttribute("cur",4);
        return SHOW_VIEW;
    }

    /**
     * 更新工厂简码
     * @param shortCode
     * @param condition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateShortCode", method = RequestMethod.POST)
    public Map<String,Object> updateFactoryShortCode(
        String shortCode,
        QuerySysUserCondition condition
    ){
        try{
            System.out.println("shortCode:"+shortCode);
            Long adminId = getFactoryAdminId();
            condition.setUserid(adminId);
            condition.setShortCode(shortCode);
            //先判断用户是否有
            Integer result1 = userService.countUserByCondition(condition);
            if(result1>0){
                return WebResult.failure("简码已存在").toMap();
            }else{
                condition.setUserid(null);
                Integer result2 = userService.countUserByCondition(condition);
                if(result2>0){
                    return WebResult.failure("简码已被占用").toMap();
                }else{
                    SysUser user = userService.selectByPrimaryKey(adminId);
                    user.setShortcode(shortCode);
                    userService.updateByPrimaryKeySelective(user);
                    privilegeService.getCurrentPrincipal().setUserShortCode(shortCode);
                }
            }
            return WebResult.success().toMap();
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            return WebResult.failure(e).toMap();
        }
    }

}
